package com.wuchao.basedao;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @author 吴超66
 * @version 1.0
 * @date 2022/4/11 11:49
 */
public class BaseDao1 {
    //1 连接
    public Connection getConnection(){
        Connection connection=null;
        Context ct=null;
        try {
            ct=new InitialContext();
            DataSource ds = (DataSource)ct.lookup("java:comp/env/jdbct143");
            connection=ds.getConnection();
        } catch (NamingException | SQLException e) {
            e.printStackTrace();
        }
        return  connection;
    }
    //2关闭
    public  void closeAll(Connection connection, PreparedStatement preparedStatement,
                          ResultSet resultSet){
        try {
            if (connection!=null&!connection.isClosed()){
                connection.close();
            }
            if (preparedStatement!=null&!preparedStatement.isClosed()){
                preparedStatement.close();
            }
            if (resultSet!=null&&!resultSet.isClosed()){
                resultSet.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
